package com.campus.web.controller;


import com.campus.app.service.CoursesService;
import com.campus.app.service.TeachersService;
import com.campus.commons.entity.Class;
import com.campus.commons.entity.Course;
import com.campus.commons.utils.MyPage;
import com.campus.commons.utils.Result;
import com.campus.web.controller.management.ClassRoomController;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * <p>
 * 课程表 前端控制器
 * </p>
 *
 * @author Rin
 * @since 2020-12-24
 */
@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private TeachersService teachersService;
    @Autowired
    private CoursesService coursesService;

    /**
     * 跳转首页
     * @param model
     * @return
     */
    @ApiOperation(value = "跳转首页")
    @GetMapping("/")
    public ModelAndView toIndex(ModelMap model) {
        model.addAttribute("class", Class.classes.values());
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("learn/course/index");
        return new ModelAndView("learn/course/index",model);
    }
    /**
     * 跳转添加
     * @return
     */
    @GetMapping("/add")
    public ModelAndView toAdd() {
        return new ModelAndView("learn/course/edit",getModelMap(false));
    }
    /**
     * 跳转编辑
     * @return
     */
    private ModelMap getModelMap(boolean edit) {
        ModelMap modelMap=new ModelMap();
        modelMap.addAttribute("edit", edit);
        modelMap.addAttribute("teachers", teachersService.getTeachersByRoleId(4).getData());
        modelMap.addAttribute("class", Class.classes.values());
        modelMap.addAttribute("classroom", ClassRoomController.getClassRooms());
    return modelMap;
    }
    @GetMapping("/edit")
    public ModelAndView toEdit() {
        return new ModelAndView("learn/course/edit",getModelMap(true));
    }
@PostMapping("/nowCourses")
@ApiOperation("动态获取当天上午||下午的课表")
    public Result getNowCourses() {
        return coursesService.getNowCourse();
    }

    //    查询
    @PostMapping("/all")
    public Result getAllCourse(MyPage myPage) {
        return coursesService.getCourse(myPage);
    }

    //    search
    @PostMapping("/search")
    public Result searchCourse(MyPage myPage,String start,String end,Integer classId) {
        return coursesService.getCourseBy(myPage, start, end, classId);
    }

    //    添加
    @PostMapping("/add")
    public Result addCourse(Course course) {
        return coursesService.addCourse(course);
    }

    //    update
    @PostMapping("/update")
    public Result updateCourse(Course course) {
        return coursesService.upCourse(course);
    }

    //    删除
    @PostMapping("/del")
    public Result delCourse(Integer id) {
        return coursesService.removeById(id)? Result.success():Result.error(null,"删除失败");
    }









}

